//
//  SY_TeacherFormViewController.m
//  FaceRecognition
//
//  Created by syun on 2017/10/26.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "SY_TeacherFormViewController.h"
#import "SY_ClassFormCustomCell.h"
#import "DateView.h"
#import "ManagerInfoModel.h"
#import "PNChart.h"
#import "SY_FormModel.h"
@interface SY_TeacherFormViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDataSource,UIPickerViewDataSource,ArrowViewDelegate,UIGestureRecognizerDelegate,reloadDataDelegate>
{
    DateView *dateV;
    BOOL isOpen;
    NSString * todayData;
    NSString * selClassID;
    NSString *sarrivalcount2;
    NSString *sarrivalcount0;
    NSString *sarrivalcount3;
    NSString *sarrivalcount1;
    
    NSDate * currentDate;
    
    NSString * todayStr;
    
    EmptyStateView *noWifiView;//无网络状态
    int requestMark;//0 代表第一个接口  1第二个 2第三个
}
@property (nonatomic, strong)UITableView *table;
@property (nonatomic, strong)UIDatePicker *pickerView;
@property (nonatomic, strong)UIControl *backControl;
@property (nonatomic, strong)UIButton * dataButton;

@property (nonatomic, strong)UITableView *classTable;
@property (nonatomic ,strong) UIView * backView;
@property (nonatomic, strong)UIButton * titleView;
@property (nonatomic, strong)NSMutableArray * classArray;
@property (nonatomic ,strong) UILabel * emptyLabel;

@property (nonatomic,strong) PNPieChart *chartView;
@property (nonatomic,strong) NSMutableArray * dataArray;

@end

@implementation SY_TeacherFormViewController

#pragma initView

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (NSMutableArray *)classArray
{
    if (_classArray == nil) {
        
        _classArray = [NSMutableArray array];
    }
    
    return _classArray;
}

- (UITableView *)table
{
    if (_table == nil) {
        
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.showsVerticalScrollIndicator = NO;
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 284 +(SCREENHEIGH - 348)/2, VIEW_WIDTH, 15)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        [_table addSubview:_emptyLabel];
    }
    
    return _table;
    
}

- (UITableView *)classTable
{
    if (_classTable == nil) {
        
        _classTable = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, SCREENWIDTH, 100) style:UITableViewStylePlain];
        _classTable.delegate = self;
        _classTable.dataSource = self;
        _classTable.hidden = YES;
        _classTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _classTable;
}
- (UIView *)backView {
    
    if (_backView == nil) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight-SafeAreaBottomHeight)];
        [_backView setBackgroundColor:[UIColor blackColor]];
        _backView.alpha = 0.5;
        _backView.hidden = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeClassTable)];
        [_backView addGestureRecognizer:tap];
    }
    return _backView;
}

- (void)removeClassTable {
    
    [_titleView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"] forState:UIControlStateNormal];
    [self.classTable setHidden:YES];
    [self.backView setHidden:YES];
    
}

- (void)changeSubject:(UIButton *)sender
{
    if (self.classTable.hidden == YES) {
        
        [self.classTable setHidden:NO];
        [self.backView setHidden:NO];
        
        if (self.classArray.count > 1) {
            
            [_titleView setImage:[UIImage imageNamed:@"record_head_classchoose_up_"] forState:UIControlStateNormal];
        }else
        {
            [_titleView setImage:nil forState:UIControlStateNormal];
        }
        
        
    }else
    {
        [self.classTable setHidden:YES];
        [self.backView setHidden:YES];
        
        if (self.classArray.count > 1) {
            [_titleView setImage:[UIImage imageNamed:@"record_head_classchoose_down_"] forState:UIControlStateNormal];
        }else
        {
            [_titleView setImage:nil forState:UIControlStateNormal];
        }
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    currentDate = [NSDate dateWithTimeIntervalSince1970:time];
    
    sarrivalcount2 = @"0";
    sarrivalcount0=  @"0";
    sarrivalcount3=  @"0";
    sarrivalcount1=  @"0";
    
    todayData = [self getCurrentTime];
    todayStr = todayData;
    [self configureNav];
    [self getManagerClasses];
    
    noWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-64-175)/2, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    noWifiView.userInteractionEnabled= YES;
    noWifiView.delegate = self;
    [self.view addSubview:noWifiView];

    // Do any additional setup after loading the view.
}
- (void)reloadCurrentData
{
    self.table.hidden = NO;
    [noWifiView hideEmptyImage];
    
    if (requestMark == 0) {
        
        [self getManagerClasses];
    }else
    {
        [self requestClassReportData];
        [self requestClassStudentArrList];
    }
}

-(void)configureNav{
    
//    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
//    self.navigationItem.leftBarButtonItem = backBtn;
   
//    UIView * titleBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 20)];
   
    self.navBar.centerImg.hidden = NO;
    _titleView = [UIButton buttonWithType:UIButtonTypeCustom];
    _titleView.frame = CGRectMake(0, 0, 120, 44);
    
    [_titleView setTitleColor:RGBCOLOR(59, 59, 59) forState:UIControlStateNormal];
    _titleView.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.navBar.centerImg addSubview:_titleView];
    
//    self.navBar.navigationItem.titleView = titleBackView;
   
   
   
      [self.view addSubview:self.table];
}

#pragma UItableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.table) {
        
        return 2;
        
    }else
    {
        return 1;
    }
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.table) {
        
        if (section == 0) {
            
            return 2;
        }else{
            
            return self.dataArray.count + 1;
            
        }
    }else
    {
        return self.classArray.count;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.table) {
        
        return 10;
    }else
    {
        return 0;
    }

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10)];
    view.backgroundColor = [UIColor colorWithHexString:@"f4f5f7"];
    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.table) {
        
        if (indexPath.section == 0) {
            
            if (indexPath.row == 0) {
                
                return 40;
            }else
            {
                return 184;
            }
        }else
        {
            if (indexPath.row == 0) {
                
                return 40;
            }else
            {
                return 39;
            }
        }
    }else
    {
        return 39;
    }
 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.table) {
        
        if (indexPath.section == 0) {
            
            if (indexPath.row == 0) {
                
                UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
                if (cell == nil) {
                    
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    
                    UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 12, 150, 15)];
                    markLabel.text = @"班级人员出勤";
                    markLabel.font = [UIFont systemFontOfSize:16];
                    markLabel.textColor = [UIColor colorWithHexString:@"000000"];
                    [cell.contentView addSubview:markLabel];
                    
                    _dataButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    _dataButton.frame = CGRectMake(SCREENWIDTH-12-150, 0, 150, 44);
                    
                    [_dataButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
                    [_dataButton setTitleColor:[UIColor colorWithHexString:@"979797"] forState:UIControlStateNormal];
                    [_dataButton addTarget:self action:@selector(chooseDataAction:) forControlEvents:UIControlEventTouchUpInside];
                    
                    [_dataButton setImageEdgeInsets:UIEdgeInsetsMake(0, 138, 0, 0)];
                    [_dataButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
                    [_dataButton setImage:[UIImage imageNamed:@"record_head_classchoose_down_"] forState:UIControlStateNormal];
                    _dataButton.titleLabel.font = [UIFont systemFontOfSize:14];
                    
                    if ([todayStr isEqualToString:todayData]) {
                        
                        [_dataButton setTitle:[NSString stringWithFormat:@"今日(%@)",todayStr] forState:UIControlStateNormal];
                        
                    }else
                    {
                        [_dataButton setTitle:todayData forState:UIControlStateNormal];
                    }
                    
                    [cell.contentView addSubview:_dataButton];
                    
                    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(12, 39.5, SCREENWIDTH-12, 0.5)];
                    line.backgroundColor = [UIColor colorWithHexString:@"dedede"];
                    [cell.contentView addSubview:line];
                    
                    
                }
                return cell;
                
            }else
            {
                UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell2"];
                if (cell == nil) {
                    
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
                    
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    
                }
                
                for (UIView *view in cell.contentView.subviews) {
                    
                    
                    [view removeFromSuperview];
                    
                }
                
                NSMutableArray *arrM = [NSMutableArray array];
                
                NSArray * numArr = @[sarrivalcount2,sarrivalcount0,sarrivalcount3,sarrivalcount1];
                NSArray * colorArr = @[@"e8b8f9",@"b786ca",@"ffb7cf",@"ffd6b3"];
                
                NSString * txt2 = [NSString stringWithFormat:@"迟到 %@",sarrivalcount2];
                NSString * txt0 = [NSString stringWithFormat:@"旷课 %@",sarrivalcount0];
                NSString * txt3 = [NSString stringWithFormat:@"请假 %@",sarrivalcount3];
                NSString * txt1 = [NSString stringWithFormat:@"正常 %@",sarrivalcount1];
                
                NSArray * txtArr = @[txt2,txt0,txt3,txt1];
                
                NSLog(@"%@  %@  %@ ",numArr,colorArr,txtArr);
                
                for (int i = 0; i<4; i++) {
                    
                    NSString * colorStr = [NSString stringWithFormat:@"%@",[colorArr objectAtIndex:i]];
                    
                    PNPieChartDataItem *item =  [PNPieChartDataItem dataItemWithValue:[[numArr objectAtIndex:i] integerValue] color:[UIColor colorWithHexString:colorStr] description:[txtArr objectAtIndex:i]];
                    
                    [arrM addObject:item];
                }
                
                NSLog(@"%@",arrM);
                
                _chartView = [[PNPieChart alloc] initWithFrame:CGRectMake(30, 15, 140, 140) items:arrM];
                _chartView.descriptionTextFont  = [UIFont fontWithName:@"Avenir-Medium" size:11.0];
                _chartView.descriptionTextShadowColor = [UIColor clearColor];
                _chartView.showAbsoluteValues = NO;
                _chartView.showOnlyValues = YES;
//                _chartView.displayAnimated = NO;
                [_chartView strokeChart];
                
                
                _chartView.legendStyle = PNLegendItemStyleStacked;
                _chartView.legendFont = [UIFont systemFontOfSize:15];
                
                UIView *legend = [_chartView getLegendWithMaxWidth:200];
                [legend setFrame:CGRectMake(230, 64, legend.frame.size.width, legend.frame.size.height)];
                [cell.contentView addSubview:legend];
                
                [cell.contentView addSubview:_chartView];
                return cell;
            }
            
        }else
        {
            if (indexPath.row == 0) {
                
                UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
                if (cell == nil) {
                    
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
                    
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 12, 150, 15)];
                    markLabel.text = @"班级月统计详情";
                    markLabel.textColor = [UIColor colorWithHexString:@"000000"];
                    markLabel.font = [UIFont systemFontOfSize:16];
                    [cell.contentView addSubview:markLabel];
                    
                    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(12, 39.5, SCREENWIDTH-12, 0.5)];
                    line.backgroundColor = [UIColor colorWithHexString:@"dedede"];
                    [cell.contentView addSubview:line];
                }
                
                return cell;
                
            }else
            {
                SY_FormModel * model = [self.dataArray objectAtIndex:indexPath.row-1];
                
                SY_ClassFormCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SY_ClassFormCustomCell"];
                if (cell == nil) {
                    
                    cell = [[SY_ClassFormCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SY_ClassFormCustomCell"];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.timeLable.text = model.date;
                cell.lateNum.text = [NSString stringWithFormat:@"%@迟到",model.sarrivalcount2];
                cell.absentNum.text = [NSString stringWithFormat:@"%@旷课",model.sarrivalcount0];
                cell.leaveNum.text = [NSString stringWithFormat:@"%@请假",model.sarrivalcount3];
                
                return cell;
            }
            
        }
    }else{
        
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell3"];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
            cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        }

       UIView * line = [[UIView alloc]initWithFrame:CGRectMake(15, 38.5, SCREENWIDTH-15, 0.5)];
       line.backgroundColor = [UIColor colorWithHexString:@"dedede"];
       [cell.contentView addSubview:line];
        ManagerInfoModel * model = [self.classArray objectAtIndex:indexPath.row];
        cell.textLabel.text = model.classname;
       
        
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.classTable) {
        
        
        ManagerInfoModel * model = [self.classArray objectAtIndex:indexPath.row];
        selClassID = model.classid;
        [self requestClassReportData];
        [self requestClassStudentArrList];
        [_titleView setTitle:model.classname forState:UIControlStateNormal];
        CGSize size = [self getButtonStringWidth:model.classname];
        [self getButton:_titleView LeftStringSize:size AndRightImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
        [_classTable setHidden: YES];
        [_backView setHidden:YES];
    }
}

- (void)requestClassReportData
{
    NSMutableDictionary *requestDic = [NSMutableDictionary dictionary];
    
    [requestDic setObject:todayData forKey:@"date"];
    [requestDic setObject:selClassID forKey:@"classid"];
    [Utils Java_get:@"api/getScClassReport" parameters:requestDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        NSLog(@"jsonObject = %@",jsonObject);
        if (arr.count > 0) {
            
            sarrivalcount2 = [[arr objectAtIndex:0]objectForKey:@"sarrivalcount2"];
            sarrivalcount0=  [[arr objectAtIndex:0]objectForKey:@"sarrivalcount0"];
            sarrivalcount3=  [[arr objectAtIndex:0]objectForKey:@"sarrivalcount3"];
            sarrivalcount1=  [[arr objectAtIndex:0]objectForKey:@"sarrivalcount1"];
        }else
        {
            sarrivalcount2 = @"0";
            sarrivalcount0=  @"0";
            sarrivalcount3=  @"0";
            sarrivalcount1=  @"0";
        }
        
        [self.table reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self.table.hidden = YES;
            [self->noWifiView showEmptyImage];
            requestMark = 1;
        }
    }];
}

- (void)requestClassStudentArrList
{

    NSMutableDictionary *requestDic = [NSMutableDictionary dictionary];
    [requestDic setObject:selClassID forKey:@"classid"];
    [requestDic setObject:todayData forKey:@"date"];
    [Utils Java_get:@"api/getClassStudentTotleList" parameters:requestDic success:^(NSInteger recode, id jsonObject) {
        
        [self.dataArray removeAllObjects];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"arr = %@",arr);
        if ([arr isKindOfClass:[NSNull class]] || ![arr isKindOfClass:[NSArray class]] || arr.count == 0) {
            
            [_emptyLabel setHidden:NO];
            [self.table reloadData];
            return;
        }else
        {
            [_emptyLabel setHidden:YES];
            for ( NSDictionary * dic in arr) {
                
                SY_FormModel * model = [SY_FormModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            
        }
        
        [self.table reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self.table.hidden = YES;
            [self->noWifiView showEmptyImage];
            requestMark = 2;
        }
    }];
}
#pragma mark -Request
//获取老师身份下所有管理班级(作为班主任的班级)
- (void)getManagerClasses{
    //0是老师身份，1是管理员身份
    NSMutableDictionary * paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    [paramDic setObject:@"0" forKey:@"type"];
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
       
        NSLog(@"array = %@",array);
       
        [self.classArray removeAllObjects];
        
        if (array.count == 0) {
            
            return;
        }
        
        self.classArray = [ManagerInfoModel mj_objectArrayWithKeyValuesArray:array];
        
        ManagerInfoModel * model = [self.classArray objectAtIndex:0];
        selClassID = model.classid;
        
        [self requestClassReportData];
        [self requestClassStudentArrList];
        
        [_titleView setTitle:model.classname forState:UIControlStateNormal];
        [_titleView addTarget:self action:@selector(changeSubject:) forControlEvents:UIControlEventTouchUpInside];
        CGSize size = [self getButtonStringWidth:model.classname];
        
        if (self.classArray.count >1) {
            
        [self getButton:_titleView LeftStringSize:size AndRightImage:[UIImage imageNamed:@"record_head_classchoose_down_"]];
           
         [self.view addSubview:self.backView];
         [self.view addSubview:self.classTable];
           
         if (39*self.classArray.count > SCREENHEIGH - SafeAreaTopHeight-SafeAreaBottomHeight- 44) {
              self.classTable.scrollEnabled = YES;
              [self.classTable setHeight:SCREENHEIGH - SafeAreaTopHeight-SafeAreaBottomHeight - 44];
           }else {
              
              [self.classTable setHeight:39 * self.classArray.count];
              self.classTable.scrollEnabled = NO;
           }
        }

       
        
    } failure:^(NSInteger recode, NSError *error) {
        
        if (error.code == -1009) {
            self.table.hidden = YES;
            [self->noWifiView showEmptyImage];
            requestMark = 0;
        }
    }];
    
}

- (void)chooseDataAction:(UIButton *)sender
{
    isOpen = !isOpen;
    
    if (isOpen == YES) {
        [_dataButton setImage:[UIImage imageNamed:@"record_head_classchoose_up_"] forState:UIControlStateNormal];
    }
    
    dateV = [[DateView alloc] initAddSuperView:self.view canceltype:1];
    __weak SY_TeacherFormViewController * weakself = self;
    dateV.aDelegate = weakself;
    dateV.datePicker.maximumDate = currentDate;
    
    dateV.getDate = ^(NSString *dateStr){
        NSMutableString * dateS = [NSMutableString stringWithFormat:@"%@",dateStr];
        NSString * date = [[dateS componentsSeparatedByString:@" "] firstObject];
        
        todayData = date;
        
        if ([todayStr isEqualToString:todayData]) {
            
            [weakself.dataButton setTitle:[NSString stringWithFormat:@"今日(%@)",todayStr] forState:UIControlStateNormal];
            
        }else
        {
            [weakself.dataButton setTitle:todayData forState:UIControlStateNormal];
        }
        
        [weakself requestClassReportData];
        [weakself requestClassStudentArrList];
    };
    
}

- (void)changeArrowDirection:(BOOL)Flag
{
    if (Flag == YES) {
        
        [_dataButton setImage:[UIImage imageNamed:@"record_head_classchoose_down_"] forState:UIControlStateNormal];
    }
}

- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

//获取字符串的宽度并

- (CGSize)getButtonStringWidth:(NSString *)string
{
    CGSize oriSize = CGSizeMake(10000, 20);
    
    CGSize size = [string boundingRectWithSize:oriSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
    return size;
}
//设置UIbutton左文字右图片
- (void)getButton:(UIButton *)button  LeftStringSize:(CGSize)size AndRightImage:(UIImage *)image
{
    [button setImage:image forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, image.size.width)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0,size.width+3, 0,- (size.width+3))];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
